home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Misc / PixPluginSample / PIXPluginSample.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  12.1 KB  |  353 lines

  1. //==================================================================================================
  2. // PIXPluginSample.cpp
  3. //
  4. // Copyright (c) Microsoft Corporation, All rights Reserved
  5. //==================================================================================================
  6.  
  7. #include <Windows.h>
  8. #include <stdio.h>
  9. #include <PIXPlugin.h>
  10. #include "Resource.h"
  11.  
  12. // Indicate the version of this plugin
  13. #define SAMPLE_PLUGIN_VERSION 0x0102
  14.  
  15.  
  16. // Macro for determining the number of elements in an array
  17. #ifndef ARRAY_LENGTH
  18. #define ARRAY_LENGTH(x)  (sizeof(x)/sizeof((x)[0]))
  19. #endif
  20.  
  21.  
  22. // Unique identifiers for each counter implemented in this plugin
  23. enum MYCOUNTERID
  24. {
  25.     CTR_FIRST,
  26.     CTR_MOUSEPOSXY = CTR_FIRST,
  27.     CTR_MOUSEPOSX,
  28.     CTR_MOUSEPOSY,
  29.     CTR_KEYBOARDCAPTURE,
  30.     CTR_COUNT
  31. };
  32.  
  33. //==================================================================================================
  34. // pixCounterSet - Lists all counters exposed by this plugin.  It is used by PIXGetCounterInfo.  In
  35. //                 this example we provide a fixed set of counters, but a plugin could use a 
  36. //                 variable set instead.
  37. //==================================================================================================
  38. PIXCOUNTERINFO pixCounterSet[] =
  39. {
  40.     { CTR_MOUSEPOSXY,       L"MousePosXY",      PCDT_STRING },
  41.     { CTR_MOUSEPOSX,        L"MousePosX",       PCDT_INT },
  42.     { CTR_MOUSEPOSY,        L"MousePosY",       PCDT_INT },
  43.     { CTR_KEYBOARDCAPTURE,  L"KeyboardCapture", PCDT_STRING },
  44. };
  45.  
  46.  
  47. //==================================================================================================
  48. // pixCounterDesc - Description strings for all counters.  It is used by PIXGetCounterDesc.  This
  49. //                  example shows how to use constant strings embedded in the code.  Another 
  50. //                  approach would be to read the description strings from resources.
  51. //==================================================================================================
  52. WCHAR* pixCounterDesc[] = 
  53. {
  54.     L"A string indicating the X and Y positions of the mouse at this frame.",
  55.     L"X coordinate of the mouse position at this frame.",
  56.     L"Y coordinate of the mouse position at this frame.",
  57.     L"A string indicating all keys pressed at this frame."
  58. };
  59.  
  60.  
  61. //==================================================================================================
  62. // MousePosXYBegin - Called at the beginning of the experiment if the CTR_MOUSEPOSXY counter is 
  63. //                   active.
  64. //==================================================================================================
  65. BOOL MousePosXYBegin( const WCHAR *pApplication )
  66. {
  67.     WCHAR ctemp[1024];
  68.  
  69.     _snwprintf(ctemp, ARRAY_LENGTH(ctemp), L"Exp MousePosXY begin %s\r\n", pApplication);
  70.  
  71.     OutputDebugString(ctemp);
  72.  
  73.     return TRUE;
  74. }
  75.  
  76.  
  77. //==================================================================================================
  78. // MousePosXYFrame - Get the mouse position (x,y) and return it as a string to PIX.
  79. //==================================================================================================
  80. WCHAR g_szPosXY[MAX_PATH] = {0};
  81. BOOL MousePosXYFrame( DWORD *pdwReturnBytes, BYTE **ppReturnData )
  82. {
  83.     //get the mouse position
  84.     POINT Point;
  85.     if(!GetCursorPos(&Point))
  86.     {
  87.         (*pdwReturnBytes) = 0;  //don't return any data if we fail
  88.         return FALSE;
  89.     }
  90.  
  91.     swprintf(g_szPosXY, L"Mouse: %d,%d", Point.x, Point.y); //this will never be larger than MAX_PATH chars
  92.  
  93.     *ppReturnData = (BYTE*) g_szPosXY;
  94.     *pdwReturnBytes = ((DWORD)wcslen(g_szPosXY)+1) * sizeof(WCHAR);
  95.  
  96.     return TRUE;
  97. }
  98.  
  99.  
  100. //==================================================================================================
  101. // MousePosXYEnd
  102. //==================================================================================================
  103. BOOL MousePosXYEnd()
  104. {
  105.     return TRUE;
  106. }
  107.  
  108.  
  109. //==================================================================================================
  110. // MousePosXBegin
  111. //==================================================================================================
  112. BOOL MousePosXBegin( const WCHAR *pszApplication )
  113. {
  114.     WCHAR ctemp[1024];
  115.  
  116.     _snwprintf(ctemp, ARRAY_LENGTH(ctemp), L"Exp MousePosX begin %s\r\n", pszApplication);
  117.  
  118.     OutputDebugString(ctemp);
  119.  
  120.     return TRUE;
  121. }
  122.  
  123.  
  124. //==================================================================================================
  125. // MousePosXFrame - Get the mouse position (x) and return it as an INT to PIX.
  126. //==================================================================================================
  127. INT g_iPosX = 0;
  128. BOOL MousePosXFrame( DWORD *pdwReturnBytes, BYTE **ppReturnData )
  129. {
  130.     //get the mouse position
  131.     POINT Point;
  132.     if(!GetCursorPos(&Point))
  133.     {
  134.         (*pdwReturnBytes) = 0;  //don't return any data if we fail
  135.         return FALSE;
  136.     }
  137.  
  138.     g_iPosX = Point.x;
  139.  
  140.     *ppReturnData = (BYTE*)&g_iPosX;
  141.     *pdwReturnBytes = sizeof(INT);
  142.  
  143.     return TRUE;
  144. }
  145.  
  146.  
  147. //==================================================================================================
  148. // MousePosXEnd
  149. //==================================================================================================
  150. BOOL MousePosXEnd()
  151. {
  152.     return TRUE;
  153. }
  154.  
  155.  
  156. //==================================================================================================
  157. // MousePosYBegin
  158. //==================================================================================================
  159. BOOL MousePosYBegin( const WCHAR *pszApplication )
  160. {
  161.     WCHAR ctemp[1024];
  162.  
  163.     _snwprintf(ctemp, ARRAY_LENGTH(ctemp), L"Exp MousePosY begin %s\r\n", pszApplication);
  164.  
  165.     OutputDebugString(ctemp);
  166.  
  167.     return TRUE;
  168. }
  169.  
  170.  
  171. //==================================================================================================
  172. // MousePosYFrame - get the mouse position (y) and return it as an INT to PIX
  173. //==================================================================================================
  174. INT g_iPosY = 0;
  175. BOOL MousePosYFrame( DWORD* pdwReturnBytes, BYTE** ppReturnData )
  176. {
  177.     //get the mouse position
  178.     POINT Point;
  179.     if(!GetCursorPos(&Point))
  180.     {
  181.         (*pdwReturnBytes) = 0;  //don't return any data if we fail
  182.         return FALSE;
  183.     }
  184.  
  185.     g_iPosY = Point.y;
  186.  
  187.     *ppReturnData = (BYTE*)&g_iPosY;
  188.     *pdwReturnBytes = sizeof(INT);
  189.  
  190.     return TRUE;
  191. }
  192.  
  193.  
  194. //==================================================================================================
  195. // MousePosYEnd
  196. //==================================================================================================
  197. BOOL MousePosYEnd()
  198. {
  199.     return TRUE;
  200. }
  201.  
  202.  
  203. //==================================================================================================
  204. // KeyboardCaptureBegin
  205. //==================================================================================================
  206. BOOL KeyboardCaptureBegin( const WCHAR *pszApplication )
  207. {
  208.     WCHAR ctemp[1024];
  209.  
  210.     _snwprintf(ctemp, ARRAY_LENGTH(ctemp), L"Exp Keyboard Capture begin %s\r\n", pszApplication);
  211.  
  212.     OutputDebugString(ctemp);
  213.  
  214.     return TRUE;
  215. }
  216.  
  217.  
  218. //==================================================================================================
  219. // KeyboardCaptureFrame - get the keys currently being pressed
  220. //==================================================================================================
  221. WCHAR g_szKeys[513] = {0};
  222. BOOL KeyboardCaptureFrame( DWORD* pdwReturnBytes, BYTE** ppReturnData )
  223. {
  224.     BOOL bKeyFound = FALSE;
  225.     ZeroMemory(g_szKeys,513*sizeof(WCHAR));
  226.     int iStringIndex = 0;
  227.     for(int i=0x41; i<0x5b; i++) // Just look at A-Z
  228.     {
  229.         if(GetAsyncKeyState(i))
  230.         {
  231.             if(iStringIndex < 512)
  232.             {
  233.                 g_szKeys[iStringIndex] = (WCHAR)i;
  234.                 iStringIndex ++;
  235.                 g_szKeys[iStringIndex] = ' ';
  236.                 iStringIndex ++;
  237.  
  238.                 bKeyFound = TRUE;
  239.             }
  240.         }
  241.     }
  242.  
  243.     *ppReturnData = (BYTE*)g_szKeys;
  244.     *pdwReturnBytes = ((DWORD)wcslen(g_szKeys)+1) * sizeof(WCHAR);
  245.  
  246.     return TRUE;
  247. }
  248.  
  249.  
  250. //==================================================================================================
  251. // KeyboardCaptureEnd
  252. //==================================================================================================
  253. BOOL KeyboardCaptureEnd()
  254. {
  255.     return TRUE;
  256. }
  257.  
  258.  
  259. //==================================================================================================
  260. // PIXGetPluginInfo
  261. //==================================================================================================
  262. BOOL WINAPI PIXGetPluginInfo( PIXPLUGININFO* pPIXPluginInfo )
  263. {
  264.     // If we want the hInst of the app, we can get it from pPIXPluginInfo->hinst
  265.  
  266.     //
  267.     // Fill in our information and give it to PIX
  268.     //
  269.     pPIXPluginInfo->iPluginVersion = SAMPLE_PLUGIN_VERSION; //version of this plugin
  270.  
  271.     pPIXPluginInfo->iPluginSystemVersion = PIX_PLUGIN_SYSTEM_VERSION; //version of PIX we are designed to work with
  272.     
  273.     pPIXPluginInfo->pstrPluginName = L"PIX Sample Plugin"; // name of this plugin
  274.  
  275.     return TRUE;
  276. }
  277.  
  278.  
  279. //==================================================================================================
  280. // PIXGetCounterInfo
  281. //==================================================================================================
  282. BOOL WINAPI PIXGetCounterInfo( DWORD* pdwReturnCounters, PIXCOUNTERINFO** ppCounterInfoList )
  283. {
  284.     *pdwReturnCounters = ARRAY_LENGTH(pixCounterSet);
  285.     *ppCounterInfoList = &pixCounterSet[0];
  286.     return TRUE;
  287. }
  288.  
  289.  
  290. //==================================================================================================
  291. // PIXGetCounterDesc
  292. //==================================================================================================
  293. BOOL WINAPI PIXGetCounterDesc( PIXCOUNTERID id, WCHAR** ppstrCounterDesc )
  294. {
  295.     if( id < CTR_FIRST || id >= CTR_COUNT )
  296.         return FALSE;
  297.     *ppstrCounterDesc = pixCounterDesc[id];
  298.     return TRUE;
  299. }
  300.  
  301.  
  302. //==================================================================================================
  303. // PIXBeginExperiment
  304. //==================================================================================================
  305. BOOL WINAPI PIXBeginExperiment( PIXCOUNTERID id, const WCHAR* pstrApplication )
  306. {
  307.     switch( id )
  308.     {
  309.     case CTR_MOUSEPOSXY: return MousePosXYBegin( pstrApplication );
  310.     case CTR_MOUSEPOSX: return MousePosXBegin( pstrApplication );
  311.     case CTR_MOUSEPOSY: return MousePosYBegin( pstrApplication );
  312.     case CTR_KEYBOARDCAPTURE: return KeyboardCaptureBegin( pstrApplication );
  313.     default: return FALSE;
  314.     }
  315. }
  316.  
  317.  
  318. //==================================================================================================
  319. // PIXEndFrame
  320. //==================================================================================================
  321. BOOL WINAPI PIXEndFrame( PIXCOUNTERID id, UINT iFrame, DWORD* pdwReturnBytes, BYTE** ppReturnData )
  322. {
  323.     switch( id )
  324.     {
  325.     case CTR_MOUSEPOSXY: return MousePosXYFrame( pdwReturnBytes, ppReturnData );
  326.     case CTR_MOUSEPOSX: return MousePosXFrame( pdwReturnBytes, ppReturnData );
  327.     case CTR_MOUSEPOSY: return MousePosYFrame( pdwReturnBytes, ppReturnData );
  328.     case CTR_KEYBOARDCAPTURE: return KeyboardCaptureFrame( pdwReturnBytes, ppReturnData );
  329.     default: return FALSE;
  330.     }
  331. }
  332.  
  333.  
  334. //==================================================================================================
  335. // PIXEndExperiment
  336. //==================================================================================================
  337. BOOL WINAPI PIXEndExperiment( PIXCOUNTERID id )
  338. {
  339.     switch( id )
  340.     {
  341.     case CTR_MOUSEPOSXY: return MousePosXYEnd();
  342.     case CTR_MOUSEPOSX: return MousePosXEnd();
  343.     case CTR_MOUSEPOSY: return MousePosYEnd();
  344.     case CTR_KEYBOARDCAPTURE: return KeyboardCaptureEnd();
  345.     default: return FALSE;
  346.     }
  347. }
  348.  
  349.  
  350. //==================================================================================================
  351. // eof: PIXPluginSample.cpp
  352. //==================================================================================================
  353.